package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.domain.DepartmentInfo;
import com.hushao.lims.weblims.service.DepartmentService;
import com.hushao.lims.weblims.vo.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 930588706
 * @date 18/2/3
 * 院部
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 模糊查看院部列表（分页）
     * @param departmentName
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取院部列表(分页)")
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @GetMapping
    public String listDepartmentByName(@RequestParam(value = "departmentName",required = false,defaultValue = "")String departmentName,
                                       @RequestParam(value = "async",required = false) boolean async,
                                       Pageable pageable,
                                       Model model) throws Exception{
        Page<DepartmentInfo> departmentPage=departmentService.listDepartmentByName(departmentName,pageable);
        List<DepartmentInfo> departments=departmentPage.getContent();
        model.addAttribute("page",departmentPage);
        model.addAttribute("departments",departments);
        return async==true?"departments/list ::#mainContainerRepleace":"departments/list";

    }

    /**
     * 获取添加院部界面
     * @param model
     * @return
     */
    @ApiOperation(value = "获取添加院部界面")
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @GetMapping("/add")
    public String addInterface(Model model){
        DepartmentInfo departmentInfo=new DepartmentInfo(null,null,null,null);
        model.addAttribute("department",departmentInfo);
        return "/departments/edit";
    }
    /**
     * 获取编辑院部界面
     * @param departmentId
     * @param model
     * @return
     */
    @ApiOperation(value ="获取编辑院部界面" )
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @GetMapping("/edit/{departmentId}")
    public String editInterface(@PathVariable("departmentId") Integer departmentId, Model model){
        DepartmentInfo departmentInfo=departmentService.getById(departmentId);
        model.addAttribute("department",departmentInfo);
        return "/departments/edit";
    }

    /**
     * 增加或修改院部
     * @param department
     * @param teacherNumber
     * @throws Exception
     * @return
     */
    @ApiOperation(value = "修改或增加院部服务")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @PostMapping
    public ResponseData saveOrUpdate(DepartmentInfo department, String teacherNumber)throws Exception{
        String message=departmentService.saveOrUpdate(department,teacherNumber);
        return new ResponseData(true,message);
    }

    /**
     * 删除院部
     * @param departmentId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "删除院部操作服务")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @DeleteMapping("/{departmentId}")
    public ResponseData delete(@PathVariable("departmentId") Integer departmentId)throws Exception {
        departmentService.remove(departmentId);
        return new ResponseData(true,"删除成功！");
    }
}
